# Copyright 2017 Google Inc.
# All Rights Reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Bazel BUILD for The Google C++ Testing Framework (Google Test)

load("@rules_python//python:defs.bzl", "py_library", "py_test")

licenses(["notice"])

package(default_visibility = ["//:__subpackages__"])

#on windows exclude ktest-tuple.h
cc_test(
    name = "ktest_all_test",
    size = "small",
    srcs = glob(
        include = [
            "ktest-*.cc",
            "kumotest-*.cc",
            "*.h",
            "kumotest/include/ktest/**/*.h",
        ],
        exclude = [
            "ktest-unittest-api_test.cc",
            "kumotest/src/ktest-all.cc",
            "ktest_all_test.cc",
            "ktest-death-test_ex_test.cc",
            "ktest-listener_test.cc",
            "ktest-unittest-api_test.cc",
            "kumotest-param-test-test.cc",
            "kumotest-param-test2-test.cc",
            "kumotest-catch-exceptions-test_.cc",
            "kumotest-color-test_.cc",
            "kumotest-env-var-test_.cc",
            "kumotest-failfast-unittest_.cc",
            "kumotest-filter-unittest_.cc",
            "kumotest-global-environment-unittest_.cc",
            "kumotest-break-on-failure-unittest_.cc",
            "kumotest-listener-test.cc",
            "kumotest-output-test_.cc",
            "kumotest-list-tests-unittest_.cc",
            "kumotest-shuffle-test_.cc",
            "kumotest-setuptestsuite-test_.cc",
            "kumotest-uninitialized-test_.cc",
            "kumotest-death-test_ex_test.cc",
            "kumotest-param-test-test",
            "kumotest-throw-on-failure-test_.cc",
            "kumotest-param-test-invalid-name1-test_.cc",
            "kumotest-param-test-invalid-name2-test_.cc",
        ],
    ) + select({
        "//:windows": [],
        "//conditions:default": [],
    }),
    copts = select({
        "//:windows": ["-DKTEST_USE_OWN_TR1_TUPLE=0"],
        "//conditions:default": ["-DKTEST_USE_OWN_TR1_TUPLE=1"],
    }) + select({
        # Ensure MSVC treats source files as UTF-8 encoded.
        "//:msvc_compiler": ["-utf-8"],
        "//conditions:default": [],
    }),
    includes = [
        "kumotest",
        "kumotest/include",
        "kumotest/include/internal",
        "kumotest/test",
    ],
    linkopts = select({
        "//:qnx": [],
        "//:windows": [],
        "//conditions:default": ["-pthread"],
    }),
    deps = ["//:ktest_main"],
)

# Tests death tests.
cc_test(
    name = "kumotest-death-test-test",
    size = "medium",
    srcs = ["kumotest-death-test-test.cc"],
    deps = ["//:ktest_main"],
)

cc_test(
    name = "ktest_test_macro_stack_footprint_test",
    size = "small",
    srcs = ["ktest_test_macro_stack_footprint_test.cc"],
    deps = ["//:ktest"],
)

#These kumotest tests have their own main()
cc_test(
    name = "kumotest-listener-test",
    size = "small",
    srcs = ["kumotest-listener-test.cc"],
    deps = ["//:ktest_main"],
)

cc_test(
    name = "ktest-unittest-api_test",
    size = "small",
    srcs = [
        "ktest-unittest-api_test.cc",
    ],
    deps = [
        "//:ktest",
    ],
)

cc_test(
    name = "kumotest-param-test-test",
    size = "small",
    srcs = [
        "kumotest-param-test-test.cc",
        "kumotest-param-test-test.h",
        "kumotest-param-test2-test.cc",
    ],
    deps = ["//:ktest"],
)

cc_test(
    name = "ktest_unittest",
    size = "small",
    srcs = ["ktest_unittest.cc"],
    shard_count = 2,
    deps = ["//:ktest_main"],
)

#  Py tests

py_library(
    name = "ktest_test_utils",
    testonly = 1,
    srcs = ["ktest_test_utils.py"],
    imports = ["."],
)

cc_binary(
    name = "ktest_help_test_",
    testonly = 1,
    srcs = ["ktest_help_test_.cc"],
    deps = ["//:ktest_main"],
)

py_test(
    name = "ktest_help_test",
    size = "small",
    srcs = ["ktest_help_test.py"],
    args = select({
        "//:has_absl": ["--has_absl_flags"],
        "//conditions:default": [],
    }),
    data = [":ktest_help_test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-output-test_",
    testonly = 1,
    srcs = ["kumotest-output-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-output-test",
    size = "small",
    srcs = ["kumotest-output-test.py"],
    args = select({
        "//:has_absl": [],
        "//conditions:default": ["--no_stacktrace_support"],
    }),
    data = [
        "kumotest-output-test-golden-lin.txt",
        ":kumotest-output-test_",
    ],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-color-test_",
    testonly = 1,
    srcs = ["kumotest-color-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-color-test",
    size = "small",
    srcs = ["kumotest-color-test.py"],
    data = [":kumotest-color-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-env-var-test_",
    testonly = 1,
    srcs = ["kumotest-env-var-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-env-var-test",
    size = "medium",
    srcs = ["kumotest-env-var-test.py"],
    data = [":kumotest-env-var-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-failfast-unittest_",
    testonly = 1,
    srcs = ["kumotest-failfast-unittest_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-failfast-unittest",
    size = "medium",
    srcs = ["kumotest-failfast-unittest.py"],
    data = [":kumotest-failfast-unittest_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-filter-unittest_",
    testonly = 1,
    srcs = ["kumotest-filter-unittest_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-filter-unittest",
    size = "medium",
    srcs = ["kumotest-filter-unittest.py"],
    data = [":kumotest-filter-unittest_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-global-environment-unittest_",
    testonly = 1,
    srcs = ["kumotest-global-environment-unittest_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-global-environment-unittest",
    size = "medium",
    srcs = ["kumotest-global-environment-unittest.py"],
    data = [":kumotest-global-environment-unittest_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-break-on-failure-unittest_",
    testonly = 1,
    srcs = ["kumotest-break-on-failure-unittest_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-break-on-failure-unittest",
    size = "small",
    srcs = ["kumotest-break-on-failure-unittest.py"],
    data = [":kumotest-break-on-failure-unittest_"],
    deps = [":ktest_test_utils"],
)

cc_test(
    name = "ktest_assert_by_exception_test",
    size = "small",
    srcs = ["ktest_assert_by_exception_test.cc"],
    deps = ["//:ktest"],
)

cc_binary(
    name = "kumotest-throw-on-failure-test_",
    testonly = 1,
    srcs = ["kumotest-throw-on-failure-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-throw-on-failure-test",
    size = "small",
    srcs = ["kumotest-throw-on-failure-test.py"],
    data = [":kumotest-throw-on-failure-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-list-tests-unittest_",
    testonly = 1,
    srcs = ["kumotest-list-tests-unittest_.cc"],
    deps = ["//:ktest"],
)

cc_test(
    name = "ktest_skip_test",
    size = "small",
    srcs = ["ktest_skip_test.cc"],
    deps = ["//:ktest_main"],
)

cc_test(
    name = "ktest_skip_in_environment_setup_test",
    size = "small",
    srcs = ["ktest_skip_in_environment_setup_test.cc"],
    deps = ["//:ktest_main"],
)

py_test(
    name = "ktest_skip_check_output_test",
    size = "small",
    srcs = ["ktest_skip_check_output_test.py"],
    data = [":ktest_skip_test"],
    deps = [":ktest_test_utils"],
)

py_test(
    name = "ktest_skip_environment_check_output_test",
    size = "small",
    srcs = ["ktest_skip_environment_check_output_test.py"],
    data = [
        ":ktest_skip_in_environment_setup_test",
    ],
    deps = [":ktest_test_utils"],
)

py_test(
    name = "kumotest-list-tests-unittest",
    size = "small",
    srcs = ["kumotest-list-tests-unittest.py"],
    data = [":kumotest-list-tests-unittest_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-shuffle-test_",
    srcs = ["kumotest-shuffle-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-shuffle-test",
    size = "small",
    srcs = ["kumotest-shuffle-test.py"],
    data = [":kumotest-shuffle-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-catch-exceptions-no-ex-test_",
    testonly = 1,
    srcs = ["kumotest-catch-exceptions-test_.cc"],
    deps = ["//:ktest_main"],
)

cc_binary(
    name = "kumotest-catch-exceptions-ex-test_",
    testonly = 1,
    srcs = ["kumotest-catch-exceptions-test_.cc"],
    copts = ["-fexceptions"],
    deps = ["//:ktest_main"],
)

py_test(
    name = "kumotest-catch-exceptions-test",
    size = "small",
    srcs = ["kumotest-catch-exceptions-test.py"],
    data = [
        ":kumotest-catch-exceptions-ex-test_",
        ":kumotest-catch-exceptions-no-ex-test_",
    ],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "ktest_xml_output_unittest_",
    testonly = 1,
    srcs = ["ktest_xml_output_unittest_.cc"],
    deps = ["//:ktest"],
)

cc_test(
    name = "ktest_no_test_unittest",
    size = "small",
    srcs = ["ktest_no_test_unittest.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "ktest_xml_output_unittest",
    size = "small",
    srcs = [
        "ktest_xml_output_unittest.py",
        "ktest_xml_test_utils.py",
    ],
    args = select({
        "//:has_absl": [],
        "//conditions:default": ["--no_stacktrace_support"],
    }),
    data = [
        # We invoke ktest_no_test_unittest to verify the XML output
        # when the test program contains no test definition.
        ":ktest_no_test_unittest",
        ":ktest_xml_output_unittest_",
    ],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "ktest_xml_outfile1_test_",
    testonly = 1,
    srcs = ["ktest_xml_outfile1_test_.cc"],
    deps = ["//:ktest_main"],
)

cc_binary(
    name = "ktest_xml_outfile2_test_",
    testonly = 1,
    srcs = ["ktest_xml_outfile2_test_.cc"],
    deps = ["//:ktest_main"],
)

py_test(
    name = "ktest_xml_outfiles_test",
    size = "small",
    srcs = [
        "ktest_xml_outfiles_test.py",
        "ktest_xml_test_utils.py",
    ],
    data = [
        ":ktest_xml_outfile1_test_",
        ":ktest_xml_outfile2_test_",
    ],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-setuptestsuite-test_",
    testonly = 1,
    srcs = ["kumotest-setuptestsuite-test_.cc"],
    deps = ["//:ktest_main"],
)

py_test(
    name = "kumotest-setuptestsuite-test",
    size = "medium",
    srcs = ["kumotest-setuptestsuite-test.py"],
    data = [":kumotest-setuptestsuite-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "kumotest-uninitialized-test_",
    testonly = 1,
    srcs = ["kumotest-uninitialized-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-uninitialized-test",
    size = "medium",
    srcs = ["kumotest-uninitialized-test.py"],
    data = ["kumotest-uninitialized-test_"],
    deps = [":ktest_test_utils"],
)

cc_binary(
    name = "ktest_testbridge_test_",
    testonly = 1,
    srcs = ["ktest_testbridge_test_.cc"],
    deps = ["//:ktest_main"],
)

# Tests that filtering via testbridge works
py_test(
    name = "ktest_testbridge_test",
    size = "small",
    srcs = ["ktest_testbridge_test.py"],
    data = [":ktest_testbridge_test_"],
    deps = [":ktest_test_utils"],
)

py_test(
    name = "kumotest-json-outfiles-test",
    size = "small",
    srcs = [
        "kumotest-json-outfiles-test.py",
        "ktest_json_test_utils.py",
    ],
    data = [
        ":ktest_xml_outfile1_test_",
        ":ktest_xml_outfile2_test_",
    ],
    deps = [":ktest_test_utils"],
)

py_test(
    name = "kumotest-json-output-unittest",
    size = "medium",
    srcs = [
        "kumotest-json-output-unittest.py",
        "ktest_json_test_utils.py",
    ],
    args = select({
        "//:has_absl": [],
        "//conditions:default": ["--no_stacktrace_support"],
    }),
    data = [
        # We invoke ktest_no_test_unittest to verify the JSON output
        # when the test program contains no test definition.
        ":ktest_no_test_unittest",
        ":ktest_xml_output_unittest_",
    ],
    deps = [":ktest_test_utils"],
)

# Verifies interaction of death tests and exceptions.
cc_test(
    name = "kumotest-death-test_ex_catch_test",
    size = "medium",
    srcs = ["kumotest-death-test_ex_test.cc"],
    copts = ["-fexceptions"],
    defines = ["KTEST_ENABLE_CATCH_EXCEPTIONS_=1"],
    deps = ["//:ktest"],
)

cc_binary(
    name = "kumotest-param-test-invalid-name1-test_",
    testonly = 1,
    srcs = ["kumotest-param-test-invalid-name1-test_.cc"],
    deps = ["//:ktest"],
)

cc_binary(
    name = "kumotest-param-test-invalid-name2-test_",
    testonly = 1,
    srcs = ["kumotest-param-test-invalid-name2-test_.cc"],
    deps = ["//:ktest"],
)

py_test(
    name = "kumotest-param-test-invalid-name1-test",
    size = "small",
    srcs = ["kumotest-param-test-invalid-name1-test.py"],
    data = [":kumotest-param-test-invalid-name1-test_"],
    tags = [
        "no_test_msvc2015",
        "no_test_msvc2017",
    ],
    deps = [":ktest_test_utils"],
)

py_test(
    name = "kumotest-param-test-invalid-name2-test",
    size = "small",
    srcs = ["kumotest-param-test-invalid-name2-test.py"],
    data = [":kumotest-param-test-invalid-name2-test_"],
    tags = [
        "no_test_msvc2015",
        "no_test_msvc2017",
    ],
    deps = [":ktest_test_utils"],
)
